import React from "react";
import styles from './index.less';
import queryString from 'query-string';
import { Modal, InputItem, Pagination, Icon, List, Radio } from 'antd-mobile';
import ReactDOM from 'react-dom';
import SeModal from '@/components/searchEmployee';
import { apiMaps } from '@/utils/maps';
import UserServices from '@/services/user';
import { requestSuccess, addHidden, removeHidden } from '@/utils/tools';
import { getMastId } from '@/utils/tools';
const RadioItem = Radio.RadioItem;
// 直接返回值
class qwcModal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: true,
      message: props.message,
      content: props.content,
      success: props.success,
      cancel: props.cancel,
      wbsCodeList: [],
      wbsCodeTotal: 0,
      wbsCodeIndex: 0,
      activeCode: {},
      values: {
        posid: '',
        post1: ''
      }
    };
    window.cordovaBackEvents.push(() => {
      this.handleCancel();
    });
  }

  componentDidMount(){
    addHidden();
  }

  handleSuccess = (e) => {
    this.setState({
      visible: false,
    });
    removeHidden();
    this.state.success && this.state.success(this.state.activeCode);
  }

  handleCancel = (e) => {
    removeHidden();
    this.setState({
      visible: false,
    });
    this.state.cancel && this.state.cancel();
  }

  handleSelected = (item) => {
    this.setState({
      activeCode: item
    })
  }

  onChange = (value, key) => {
    this.setState({
      values: {
        ...this.state.values,
        [key]: value
      }
    })
  }

  handleGetWbsCodes = () => {
    const id = getMastId();

    UserServices.getWbsCodes({
      ...id,
      condition: {
        ...this.state.values
      }
    }).then(res => {
      if(requestSuccess(res)){
        const result = res.data.wbsCodeInfo;
        this.setState({
          wbsCodeList: result
        })
      }
    })
  }

  render() {
    return (
      <Modal
        visible={this.state.visible}
        wrapClassName= {styles.qwcModal}
      >
      <div className={styles.modalWrapper}>
        <div className={styles.info}>WBS Search Help</div>
          <div className={styles.list}>
            <div className={styles.item}>
              <div className={styles.des}>WBS Code</div>
              <div className={styles.value}>
                <InputItem
                  clear
                  placeholder="click input"
                  onChange={(value) => this.onChange(value, 'posid')}
                ></InputItem>
              </div>
            </div>
            <div className={styles.item} onClick={this.handleSelectedEmplooye}>
              <div className={styles.des}>WBS Desc</div>
              <div className={styles.value}>
                <InputItem
                  clear
                  placeholder="click input"
                  onChange={(value) => this.onChange(value, 'post1')}
                ></InputItem>
              </div>
            </div>
            <div className={styles.searchBtn} onClick={this.handleGetWbsCodes}>Search</div>
          </div>
          <div className={styles.results}>
            {
              this.state.wbsCodeList.length > 0 ?
              <div>
              <div className={styles.labels}>
                <div className={styles.label}>WBS Code</div>
                <div className={styles.label}>WBS Desc</div>
                <div className={styles.label}>Selected</div>
              </div>
              <List>
                {
                  this.state.wbsCodeList.map(i => (
                    <RadioItem key={i.posid} checked={this.state.activeCode.posid === i.posid} onChange={() => this.handleSelected(i)}>
                      <div className={styles.item}>
                        <div className={styles.des}>{i.posid}</div>
                        <div className={styles.des}>{i.post1}</div>
                      </div>
                    </RadioItem>
                  ))
                }
              </List>
            </div>
              :
              null
            }
          </div>
        <div className={styles.footerBtns}>
          <div onClick={this.handleSuccess}>Submit</div>
          <div onClick={this.handleCancel}>Close</div>
        </div>
      </div>
      </Modal>
    );
  }
}

export default {
  open(params){
    let div = document.createElement('div');
    document.body.appendChild(div);
    ReactDOM.render(React.createElement(qwcModal, params), div);
  }
};
